package com.heima.article.v1.controller;

import com.heima.article.entity.ApArticle;
import com.heima.article.entity.Articlebehavior;
import com.heima.article.service.ApArticleService;
import com.heima.common.constants.Constants;
import com.heima.common.dtos.FindNewsCommentsDto;
import com.heima.common.dtos.PageResult;
import com.heima.common.dtos.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.LeadException;
import com.heima.common.threadlocal.UserThreadLocalUtils;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleInfoDto;
import com.heima.model.article.dtos.ArticleLoadDTO;
import com.heima.model.behavior.dtos.CollectionBehaviorDto;
import com.heima.model.media.dtos.WmNewsResultDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.heima.common.constants.Constants.REDIS_KEY_ISCOLLECTION;

@RestController
public class ArticleController {

    @Autowired
    private ApArticleService articleService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * feign, 搜索服务做批量的数据导入
     *
     */
    @GetMapping("/api/v1/article/findByPage")
    public List<ArticleDto> findByPage(@RequestParam("page") int page, @RequestParam("size")int size) {
        return articleService.findByPage(page, size);
    }

    /**
     * feign, 搜索服务收到创建文章的消息，调用此方法查询文章的详情
     *
     */
    @GetMapping("/api/v1/article/findById")
    public ArticleDto findById(@RequestParam("articleId") long articleId){
        return articleService.findById(articleId);
    }


    /**
     * feign, 当自媒体文章审核通过以后，调用此方法保存文章，保存文章内容
     *
     */
    @PostMapping("/api/v1/article/save")
    public Long createArticle(@RequestBody WmNewsResultDTO dto){
        return articleService.createArticle(dto);
    }

    /**
     * app首页显示文章数据
     * */
    @PostMapping("/api/v1/article/load")
    public ResponseResult<List<ApArticle>> load(@RequestBody ArticleLoadDTO dto){
        List<ApArticle> list = articleService.load(dto);
        return ResponseResult.ok(list);
    }
    /**
     * 上拖动页面，查询更多文章
     * */
    @PostMapping("/api/v1/article/loadmore")
    public ResponseResult<List<ApArticle>> loadmore(@RequestBody ArticleLoadDTO dto){
        List<ApArticle> list = articleService.loadmore(dto);
        return ResponseResult.ok(list);
    }

    /**
     * 下拖动页面，查询最新文章
     * */
    @PostMapping("/api/v1/article/loadnew")
    public ResponseResult<List<ApArticle>> loadnew(@RequestBody ArticleLoadDTO dto){
        List<ApArticle> list = articleService.loadnew(dto);
        return ResponseResult.ok(list);
    }

    /**
     * 收藏
     * @return
     */
    @PostMapping("/api/v1/collection_behavior")
    public ResponseResult collection(@RequestBody CollectionBehaviorDto dto){
        Integer userId = UserThreadLocalUtils.getUserId();
        if (userId==null || ObjectUtils.isEmpty(dto)){
            throw new LeadException(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST);
        }
        Long articleId = dto.getEntryId();
        if (articleId == null){
            throw new LeadException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        String REDIS_KEY =""+articleId+userId;
        redisTemplate.opsForHash().put(REDIS_KEY,REDIS_KEY_ISCOLLECTION,dto.getOperation());
        return ResponseResult.ok();
    }

    /**
     * 行为数据会先
     * @param dto
     * @return
     */
    @PostMapping("/api/v1/article/load_article_behavior")
    public ResponseResult dataEcho(@RequestBody ArticleInfoDto dto){

        Integer userId = UserThreadLocalUtils.getUserId();
        Long articleId = dto.getArticleId();
        Integer authorId = dto.getAuthorId();
        String REDIS_KEY_AU = ""+authorId+userId;
        String REDIS_KEY_AR = ""+articleId+userId;
        Articlebehavior articlebehavior = new Articlebehavior();
        //是否点赞
        Integer isLike = (Integer) redisTemplate.opsForHash().get(REDIS_KEY_AR, Constants.REDIS_KEY_ISLIKE);

        if(isLike!=null){
            Boolean islike = behaviorResult(isLike);
            articlebehavior.setIslike(islike);
        }
        //是否不喜欢
        Integer isunlike = (Integer) redisTemplate.opsForHash().get(REDIS_KEY_AR, Constants.REDIS_KEY_ISUNLIKE);
        if (isunlike!=null){
            Boolean result = behaviorResult(isunlike);
            articlebehavior.setIsunlike(result);
        }
        //是否收藏
        Integer iscollection =(Integer) redisTemplate.opsForHash().get(REDIS_KEY_AR, Constants.REDIS_KEY_ISCOLLECTION);
        if (iscollection!=null){
            Boolean result = behaviorResult(iscollection);
            articlebehavior.setIscollection(result);
        }

        //是否关注
        Integer isfollow  = (Integer) redisTemplate.opsForHash().get(REDIS_KEY_AU, Constants.REDIS_KEY_ISFOLLOW);
        if (isfollow!=null){
            Boolean result = behaviorResult(isfollow);
            articlebehavior.setIsfollow(result);
        }

        return ResponseResult.ok(articlebehavior);

    }

    private Boolean behaviorResult(Integer a){
        if (a==0){
            return false;
        }
        return true;
    }



    @PostMapping("/api/v1/comment/manage/find_news_comments")
    public PageResult findNewsComments(@RequestBody FindNewsCommentsDto dto){
        return articleService.findNewsComments(dto);
    }


}
